#ifndef __PROTOCOL_RSU_H
#define __PROTOCOL_RSU_H

#include "t_types.h"

/**
RSU 和 PC 通讯的数据帧格式如下：
STX RSCTL DATA BCC ETX
说明如下：
字段 	描述
STX 	帧开始标志（ Start of Frame） ,取值为 FFH；
RSCTL 	串口帧序列号， 1 个字节；
		(1) RSU发送的串口帧序列号的低半字节为 8， 高半字节一般为 0～7，
			RSU 上电时发送的串口帧序号高半字节为 9；
		(2) PC 发送的串口帧序列号是将收到的串口帧序号高低半字节互换；
		(3) RSU 发送的串口帧序号为X8H， 其中 X 为 0， 1， 2， 3， 4， 5， 6， 7， 9；
		(4) PC 发送的串口帧序号为8XH，其中 X 为 0， 1， 2， 3， 4， 5， 6， 7，9；
DATA 	发送的数据；
BCC 	异或校验值，从 RSCTL 到 DATA 所有字节的异或值；
ETX 	帧结束标志，取值为 FFH

*/

#define PROTOCOL_RSU_GET_OBU_FRAME_INDEX(index) ((index & 0xf0) >> 4)
#define PROTOCOL_RSU_FRAME_INDEX_TO_PC(index) (((index & 0xf0) >> 4) | 0x80)
#define PROTOCOL_RSU_HEAD_LEN (sizeof(PROTOCOL_RSU_HEAD_S) + 2)

typedef enum _PROTOCOL_RSU_PC_CMD_E
{
	PROTOCOL_RSU_PC_CMD_C0H_INIT = 0xc0,
	PROTOCOL_RSU_PC_CMD_C1H_CONTINUE_BUSINESS = 0xc1,
	PROTOCOL_RSU_PC_CMD_C2H_STOP_BUSINESS = 0xc2,
	PROTOCOL_RSU_PC_CMD_C3H_WRITE_ENTRANCE = 0xc3,
	PROTOCOL_RSU_PC_CMD_C6H_WRITE_DEDUCTION = 0xc6,
	PROTOCOL_RSU_PC_CMD_C7H_ABNORMAL1 = 0xc7,
	PROTOCOL_RSU_PC_CMD_4CH_RF_ONOFF = 0x4C,
} PROTOCOL_RSU_PC_CMD_E;

struct _PROTOCOL_RSU_PC_C0H_INIT_S
{
	T_UINT8 CMDType;
	T_UINT32 Seconds;
	T_UINT8 Datetime[6];
	T_UINT8 LaneMode;
	T_UINT8 WaitTime;
	T_UINT8 TxPower;
	T_UINT8 PLLChannelID;
	T_UINT8 GBMode;
	T_UINT8 WSMode;
} __PACKED__;
typedef struct _PROTOCOL_RSU_PC_C0H_INIT_S PROTOCOL_RSU_PC_C0H_INIT_S;

struct _PROTOCOL_RSU_PC_C1H_CONTINUE_BUSINESS_S
{
	T_UINT8 CMDType;
	T_UINT32 OBUID;
} __PACKED__;
typedef struct _PROTOCOL_RSU_PC_C1H_CONTINUE_BUSINESS_S PROTOCOL_RSU_PC_C1H_CONTINUE_BUSINESS_S;

struct _PROTOCOL_RSU_PC_C2H_STOP_BUSINESS_S
{
	T_UINT8 CMDType;
	T_UINT32 OBUID;
} __PACKED__;
typedef struct _PROTOCOL_RSU_PC_C2H_STOP_BUSINESS_S PROTOCOL_RSU_PC_C2H_STOP_BUSINESS_S;

struct _PROTOCOL_RSU_PC_C3H_WRITE_ENTRANCE_S
{
	T_UINT8 CMDType;
	T_UINT32 OBUID;
	T_UINT8 PurchaseTime[7];
	T_UINT8 GB_001A_info[64];
} __PACKED__;
typedef struct _PROTOCOL_RSU_PC_C3H_WRITE_ENTRANCE_S PROTOCOL_RSU_PC_C3H_WRITE_ENTRANCE_S;

struct _PROTOCOL_RSU_PC_C6H_WRITE_DEDUCTION_S
{
	T_UINT8 CMDType;
	T_UINT32 OBUID;
	T_UINT8 WriteMode;
	T_UINT8 PurchaseTime[7];
	T_UINT32 ConsumeMoney;
	T_UINT8 GB_001A_info[64];
} __PACKED__;
typedef struct _PROTOCOL_RSU_PC_C6H_WRITE_DEDUCTION_S PROTOCOL_RSU_PC_C6H_WRITE_DEDUCTION_S;

struct _PROTOCOL_RSU_PC_C7H_ABNORMAL1_S
{
	T_UINT8 CMDType;
	T_UINT32 OBUID;
	T_UINT8 WrFileFlag;
	T_UINT8 GB_001A_info[64];
} __PACKED__;
typedef struct _PROTOCOL_RSU_PC_C7H_ABNORMAL1_S PROTOCOL_RSU_PC_C7H_ABNORMAL1_S;

struct _PROTOCOL_RSU_PC_4CH_RF_ONOFF_S
{
	T_UINT8 CMDType;
	T_UINT8 ControlType;
} __PACKED__;
typedef struct _PROTOCOL_RSU_PC_4CH_RF_ONOFF_S PROTOCOL_RSU_PC_4CH_RF_ONOFF_S;

typedef enum _PROTOCOL_RSU_OBU_CMD_E
{
	PROTOCOL_RSU_OBU_CMD_B0H_DEV_STATUS = 0xB0,
	PROTOCOL_RSU_OBU_CMD_B2H_OBU_NUM = 0xB2,
	PROTOCOL_RSU_OBU_CMD_B3H_OBU_PROPERTY = 0xB3,
	PROTOCOL_RSU_OBU_CMD_B4H_IC_CARD_INFO = 0xB4,
	PROTOCOL_RSU_OBU_CMD_B5H_BUSINESS_END = 0xB5,
	PROTOCOL_RSU_OBU_CMD_60H_PROCESS_STATUS = 0x60,
	PROTOCOL_RSU_OBU_CMD_D0H_COORDINATE = 0xD0,
} PROTOCOL_RSU_OBU_CMD_E;

struct _PROTOCOL_RSU_OBU_BOH_DEV_STATUS_S
{
	T_UINT8 FrameType;
	T_UINT8 RsuStatus;
	T_UINT8 PSAMNum;
	T_UINT8 GDTerminalId[6];
	T_UINT8 GBTerminalId[6];
	T_UINT8 RsuAlgId;
	T_UINT8 RSUManuID;
	T_UINT8 RSUIndividualID[3];
	T_UINT8 Area_Decode[4];
	T_UINT8 HardwareVersion[2];
	T_UINT8 SoftwareVersion[2];
	T_UINT8 MakeDate[4];
	T_UINT8 GBStatus;
	T_UINT8 GBMode;
	T_UINT8 WSMode;
	T_UINT8 Reserved[2];
} __PACKED__;
typedef struct _PROTOCOL_RSU_OBU_BOH_DEV_STATUS_S PROTOCOL_RSU_OBU_BOH_DEV_STATUS_S;

struct _PROTOCOL_RSU_OBU_B2H_OBU_NUM_S
{
	T_UINT8 CMDType;
	T_UINT32 OBUID;
	T_UINT8 OBUStatus;
	T_UINT8 OBUType;
} __PACKED__;
typedef struct _PROTOCOL_RSU_OBU_B2H_OBU_NUM_S PROTOCOL_RSU_OBU_B2H_OBU_NUM_S;

struct _PROTOCOL_RSU_OBU_B3H_OBU_PROPERTY_S
{
	T_UINT8 CMDType;
	T_UINT32 OBUID;
	T_UINT8 ErrorCode;
	T_UINT8 OBUType;
	T_UINT8 au8ContractProvider[8];
	T_UINT8 u8ContractType;
	T_UINT8 u8ContractVersion;
	T_UINT8 au8ContractSerialNumber[8];
	T_UINT8 au8ContractSignedDate[4];
	T_UINT8 au8ContractExpiredDatep[4];
	T_UINT8 u8Equipmentstatus;
	T_UINT8 au8GBOBUStatus[2];
	T_UINT8 au8EF1D_info[128];
} __PACKED__;
typedef struct _PROTOCOL_RSU_OBU_B3H_OBU_PROPERTY_S PROTOCOL_RSU_OBU_B3H_OBU_PROPERTY_S;

struct _PROTOCOL_RSU_OBU_B4H_IC_CARD_INFO_S
{
	T_UINT8 CMDType;
	T_UINT32 OBUID;
	T_UINT8 ErrorCode;
	T_UINT8 ICCType;
	T_UINT8 au8CardRestMoney[4];
	T_UINT8 au8ICCardFile0015[43];
	T_UINT8 au8GB_001A_info[64];
} __PACKED__;
typedef struct _PROTOCOL_RSU_OBU_B4H_IC_CARD_INFO_S PROTOCOL_RSU_OBU_B4H_IC_CARD_INFO_S;

struct _PROTOCOL_RSU_OBU_B5H_BUSSINESS_END_S
{
	T_UINT8 CMDType;
	T_UINT32 OBUID;
	T_UINT8 ErrorCode;
	T_UINT8 au8ICCTransSerial[2];
	T_UINT8 u8TransType;
	T_UINT8 au8TransTime[6];
	T_UINT8 u8KeyVersion;
	T_UINT8 au8PSAMTransSerial[4];
	T_UINT8 au8TAC[4];
	T_UINT8 au8WrFileTime[4];
} __PACKED__;
typedef struct _PROTOCOL_RSU_OBU_B5H_BUSSINESS_END_S PROTOCOL_RSU_OBU_B5H_BUSSINESS_END_S;

struct _PROTOCOL_RSU_OBU_60H_PROCESS_STATUS_S
{
	T_UINT8 CMDType;
	T_UINT8 au8ICCardID[8];
	T_UINT8 au8PuchaseTime[6];
	T_UINT8 u8KeyVersion;
	T_UINT8 au8PSAMTransSerial[4];
	T_UINT8 au8ICCTransSerial[2];
	T_UINT8 u8TransType;
} __PACKED__;
typedef struct _PROTOCOL_RSU_OBU_60H_PROCESS_STATUS_S PROTOCOL_RSU_OBU_60H_PROCESS_STATUS_S;

struct _PROTOCOL_RSU_OBU_D0H_COORDINATE_S
{
	T_UINT8 CMDType;
	T_UINT32 OBUID;
	T_UINT8 FrameType; // ????????
	T_UINT8 nMark;
	T_UINT8 au8Data[8];
} __PACKED__;
typedef struct _PROTOCOL_RSU_OBU_D0H_COORDINATE_S PROTOCOL_RSU_OBU_D0H_COORDINATE_S;

typedef struct _PROTOCOL_RSU_HEAD_S
{
	T_UINT8 u8FrameStartFlag;
	T_UINT8 u8FrameSerial;
	T_UINT8 au8Data[0];
	//T_UINT8 u8BCC;
	//T_UINT8 u8FrameEndFlag;
} PROTOCOL_RSU_HEAD_S;

T_INT32 PAL_PROTOCOL_RSU_Init(void);
T_INT32 PAL_PROTOCOL_RSU_DeInit(void);

#endif
